package com.chuangsi.project.admin.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chuangsi.common.constant.Constants;
import com.chuangsi.common.utils.PicExportUtil;
import com.chuangsi.framework.interceptor.annotation.RepeatSubmit;
import com.chuangsi.framework.web.controller.BaseController;
import com.chuangsi.framework.web.domain.ResultObject;
import com.chuangsi.framework.web.page.TableDataInfo;
import com.chuangsi.project.admin.dto.admin.order.*;
import com.chuangsi.project.admin.dto.app.esign.ReqESignDownloadDto;
import com.chuangsi.project.admin.dto.app.esign.ReqESignGetSignFlowDetailDto;
import com.chuangsi.project.admin.dto.app.esign.ResESignDownloadDto;
import com.chuangsi.project.admin.dto.app.esign.ResESignGetSignFlowDetailDto;
import com.chuangsi.project.admin.dto.common.SelectDto;
import com.chuangsi.project.admin.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

/**
 * @author wangshengming
 * @date 2023/3/27
 */
@Api(tags = "订单管理")
@RestController
@RequestMapping("/adminApi/order")
public class OrderController  extends BaseController {

    @Autowired
    private OrderService orderService;


    @ApiOperation(value = "财务-订单分页",notes = "YWB")
    @GetMapping("/finance/pageQuery")
    public TableDataInfo<ResFinanceOrderPageDto> financePageQuery( ReqFinanceOrderPageDto req){
        startPage();
        List<ResFinanceOrderPageDto> page = orderService.financePageQuery(req);
        return getDataTable(page);
    }


    @ApiOperation(value = "财务-导出", notes = "YWB")
    @GetMapping("/finance/export")
    //@RepeatSubmit(expireSeconds = 15)
    public void export(ReqFinanceOrderPageDto dto, HttpServletResponse response) {
        orderService.financeExport(dto,response);
    }


    @ApiOperation(value = "财务-导入", notes = "YWB")
    @PostMapping("/finance/import")
    public ResultObject<Void> financeImport(MultipartFile file) {
        //orderService.financeImport(file);
        return ResultObject.error("功能关闭");
    }

    @ApiOperation(value = "财务-上传发票",notes = "王圣铭")
    @PostMapping("/uploadInvoice")
    public ResultObject<Void> uploadInvoice(@RequestBody @Valid ReqOrderUploadInvoiceDto req){
        return orderService.uploadInvoice(req)?ResultObject.ok("上传成功"):ResultObject.error("上传失败");
    }

    @ApiOperation(value = "订单分页",notes = "王圣铭")
    @GetMapping("pageQuery")
    public TableDataInfo<ResOrderPageDto> pageQuery(ReqOrderPageDto req){
        startPage();
        List<ResOrderPageDto> page = orderService.pageQuery(req);
        return getDataTable(page);
    }

    @ApiOperation(value = "导出", notes = "ywb")
    @GetMapping("/export")
    @RepeatSubmit(interval = 15000)
    public void export(ReqOrderPageDto dto, HttpServletResponse response) {
        orderService.export(dto,response);
    }

    @ApiOperation(value = "订单详情",notes = "王圣铭")
    @PostMapping("/detail")
    public ResultObject<ResOrderDetailDto> detail(@RequestBody @Valid ReqOrderDetailDto req){
        ResOrderDetailDto res = orderService.detail(req);
        return ResultObject.ok("查询成功").setData(res);
    }

    @ApiOperation(value = "通过",notes = "王圣铭")
    @PostMapping("/pass")
    public ResultObject<Void> pass(@RequestBody @Valid ReqOrderPassDto req){
        boolean flag = orderService.pass(req);
        if(flag){return ResultObject.ok("操作成功");}
        else {return ResultObject.error("操作失败");}
    }

    @ApiOperation(value = "查询签署流程情况",notes = "王圣铭")
    @PostMapping("/getSignDetail")
    public ResultObject<ResESignGetSignFlowDetailDto> getSignDetail(@RequestBody @Valid ReqESignGetSignFlowDetailDto req){
        ResESignGetSignFlowDetailDto res = orderService.getSignDetail(req);
        return ResultObject.ok("查询成功").setData(res);
    }

    @ApiOperation(value = "下载签署文件",notes = "王圣铭")
    @PostMapping("/download")
    public ResultObject<ResESignDownloadDto> download(@RequestBody @Valid ReqESignDownloadDto req){
        ResESignDownloadDto res = orderService.download(req);
        return ResultObject.ok("获取地址成功").setData(res);
    }

    @ApiOperation(value = "删除订单",notes = "王圣铭")
    @PostMapping("/del")
    public ResultObject<Void> delOrder(@RequestBody @Valid ReqOrderDetailDto req){
        orderService.delOrder(req.getOrderId());
        return ResultObject.ok("删除成功");
    }

    @ApiOperation(value = "修改订单",notes = "王圣铭")
    @PostMapping("/updateOrder")
    public ResultObject<Void> updateOrder(@RequestBody @Valid ReqOrderUpdateAdminDto req){
        boolean flag = orderService.updateOrder(req);
        if(flag){return ResultObject.ok("修改成功");}
        else {return ResultObject.error("修改失败");}
    }

    @ApiOperation(value = "导出订单图片",notes = "王圣铭")
    @PostMapping("/downloadTest")
    public void downloadTest(HttpServletResponse response, @RequestBody @Valid ReqOrderExportIdDto req){
        //组装图片map
        Map<String, Map<String, Object>> imgsDataMap = orderService.setPicMap(req);
        // 照片存放的文件路径【相对路径，即当前项目根目录下创建，如果temp前加“\”,则会在盘符下创建temp】
        // “\\”是转义符，也可以用File.separator，防止跨平台出现错误，根据系统区分地址
        String url = System.getProperty("os.name").toLowerCase().contains("linux")?Constants.fileStrLin:Constants.fileStrWin;
        // 下载压缩文件
        PicExportUtil.downloadZipFile(imgsDataMap,response, url);
    }

    @ApiOperation(value = "添加备注", notes = "ywb")
    @PostMapping("/remark")
    public ResultObject<Void> remark(@RequestBody @Validated ReqRemarkDto req) {
        orderService.remark(req);
        return ResultObject.ok("添加备注成功");
    }

    @ApiOperation(value = "订单状态下拉选", notes = "ywb")
    @GetMapping("/orderStatus/select")
    public ResultObject<Void> orderStatusSelect() {
        List<SelectDto> selectList = orderService.orderStatusSelect();
        return ResultObject.ok().setData(selectList);
    }

    @ApiOperation(value = "订单改变状态", notes = "ywb")
    @PostMapping("/changeStatus")
    public ResultObject<Void> changeStatus(@RequestBody @Validated ReqOrderChangeStatusDto dto) {
        orderService.changeStatus(dto);
        return ResultObject.ok();
    }


}
